package org.deva.erp.common.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.deva.erp.base.service.BaseService;
import org.deva.erp.common.entity.Resource;
import org.deva.erp.common.vo.RoleResourceVO;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Project: erp<br>
 * Description: 资源管理Service，容器单例<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
@Service @Scope("singleton")
public class ResourceService extends BaseService<Resource> implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 查询资源列表，构建成一颗树
	 * @param root
	 */
	@SuppressWarnings("unchecked")
	public void buildTree(TreeNode root, Map<String, Object> query) {
		Long parentId;
		if(root.getData() instanceof String) {	//第一次
			parentId = 0L;
		} else {
			parentId = ((Resource) root.getData()).getId();
		}
		StringBuilder sql = new StringBuilder("SELECT r FROM Resource r WHERE r.parentId=:parentId ");
		Object code = query.get("code");
		if(code != null && !"".equals(code.toString())) {
			sql.append("AND r.code LIKE '" + code.toString() + "%'");
		}
		Object type = query.get("type");
		if(type != null && !"".equals(type.toString())) {
			sql.append("AND r.type = '" + type.toString() + "' ");
		}
		sql.append("ORDER BY r.code");
		List<Resource> reses = em.createQuery(sql.toString()).setParameter("parentId", parentId).getResultList();
		for(Resource res : reses) {
			buildTree(new DefaultTreeNode(res, root), query);
		}
	}
	/**
	 * 根据资源类型查找资源，没有限定类型，查找所有
	 * @param type 资源类型
	 * @return 资源集合
	 */
	@SuppressWarnings("unchecked")
	public List<Resource> getResourcesByParentId(Long parentId) {
		String sql = "SELECT r FROM Resource r WHERE r.parentId=:parentId ORDER BY r.code";
		return em.createQuery(sql).setParameter("parentId", parentId).getResultList();
	}
	/**
	 * 查找资源所属的角色及权限
	 * @param resId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<RoleResourceVO> getRolesByResId(Long resId) {
		String sql = "SELECT NEW org.deva.erp.common.vo.RoleResourceVO(rr) " +
				"FROM RoleResource rr, Role ro, Resource re " +
				"WHERE rr.res=re AND rr.role=ro AND re.id=:resId AND ro.status=1 " +
				"ORDER BY ro.code";
		return em.createQuery(sql).setParameter("resId", resId).getResultList();
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void update(Resource t) throws Exception {
		super.update(t);
	}
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void insert(Resource t) throws Exception {
		super.insert(t);
	}
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void delete(Resource t) throws Exception {
		super.delete(t);
	}
	
}
